/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.dao4mysql;

import com.zjdt.epserver.db.dao.TruckDao;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class TruckDaoImpl implements TruckDao{

    static Logger logger = Logger.getLogger(TruckDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public TruckDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }       
    
    @Override
    public boolean doCreate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "insert into truck(driverId,name,plateNumber,type,capacity,brand,color,note,photo,licensePhoto) values(?,?,?,?,?,?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from truck where id=?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update truck set name=?,plateNumber=?,type=?,capacity=?,brand=?,color=?,note=?,photo=?,licensePhoto=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }    
    
    //���³�����Ƭ
    @Override
    public boolean doUpdatePhotoById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update truck set photo=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }    
    
    @Override
    public boolean doUpdateLicensePhotoById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update truck set licensePhoto=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public Map<String, Object> findById(int id) {
        String sql = null;
        try{
            sql = "select * from  truck where id = ?";
            return jdbcTemplate.queryForMap(sql,id);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }        
    }

    @Override
    public List<Map<String, Object>> findAllByDriverId(int id) {        
        String sql = "select * from truck where driverId = ?";
        return jdbcTemplate.queryForList(sql,id);
    }    
}
